package com.xbongbong.saas.model;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.IModel;
import com.xbongbong.saas.domain.entity.CustomerUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import org.elasticsearch.action.support.WriteRequest;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/25 10:18
 * @since v1.0
 */
public interface CustomerUserModel extends IModel<CustomerUserEntity> {

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return java.util.List<com.xbongbong.saas.domain.entity.CustomerUserEntity>
     * @author feng.zheng
     * @date 2019/2/16 14:22
     */
    @Override
    List<CustomerUserEntity> findEntitys(Map<String, Object> param);

    /**
     * 保存实体
     *
     * @param customerUserEntity 客户协同人团队实体
     * @return java.lang.Integer
     * @throws XbbException 数据库异常
     * @author feng.zheng
     * @date 2019/2/14 16:42
     */
    Integer save(CustomerUserEntity customerUserEntity) throws XbbException;

    /**
     * 获取单个实体
     *
     * @param key    id主键
     * @param corpid 公司id
     * @return com.xbongbong.saas.domain.entity.CustomerUserEntity
     * @author feng.zheng
     * @date 2019/2/14 16:43
     */
    CustomerUserEntity getByKey(Long key, String corpid);

    /**
     * Description: 获取客户负责人/协同人的id
     * @param corpid 公司id
     * @param customerIds 客户id
     * @param isMain 是否为负责人 1 负责人 0协同人 null所有负责人合协同人
     * @return java.util.List<java.lang.String> 客户负责人/协同人id
     * @author 魏荣杰
     * @date 2019/2/18 14:43
     * @since v1.0
     * Modified by:
     */
    List<String> getCustomerUserIdList(String corpid, List<Long> customerIds, Integer isMain);
    /**
     * 获取协同团队的业务数据
     * @param corpid 公司id
     * @param isMain 负责或协同
     * @param userId 用户id
     * @return List<Long>
     */
    List<Long> getDataIdList(String corpid, Integer isMain, List<String> userId);

    /**
     * 公海规则查询负责人id
     * @param corpid
     * @param isMain
     * @param userId
     * @param startId data_id > startId
     * @param pageNum limit pageNum
     * @return
     */
    List<Long> getDataIdList4Rule(String corpid, Integer isMain, String userId, Long startId, Integer pageNum);

    /**
     * 协同人批量升级
     * @param corpid
     * @param customerUsers
     */
    void batchUpgrade(String corpid, List<CustomerUserEntity> customerUsers, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 协同人批量升级
     * @param corpid
     * @param customerUsers
     * @param del
     * @param refreshPolicy
     */
    void deleteBatch(String corpid, List<CustomerUserEntity> customerUsers, WriteRequest.RefreshPolicy refreshPolicy, Integer del) throws XbbException;

    /**
     * 批量插入
     * @param customerUserEntityList
     */
    void insertBatch(List<CustomerUserEntity> customerUserEntityList, boolean isImport) throws XbbException;

    /**
     * 退回公海池解散团队
     * @param corpid
     * @param customerIdList
     */
    void disbandedTeam(String corpid, List<Long> customerIdList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 批量剔除团队
     * @param corpid
     * @param customerIdList 客户id
     * @param userIdList
     */
    void batchDeleteByCustomerId(String corpid, List<Long> customerIdList, List<String> userIdList) throws XbbException;

    void batchDeleteTeam(String corpid, List<Long> customerIdList, List<String> userIdList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 批量剔除团队
     * @param corpid
     * @param removeIdList 客户团队表idList
     */
    Map<Long, Set<String>> batchDeleteById(String corpid, List<Long> removeIdList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 删除团队成员
     * @param customerUser
     */
    void delete(CustomerUserEntity customerUser) throws XbbException;

    /**
     * 查询基础列表
     * @param map
     * @return
     */
    List<UserTeamEntity> findBaseEntitys(Map<String,Object> map);

    /**
     * 批量更新
     * @author 吴峰
     * @date 2019/3/14 18:59
     * @param list
     * @param corpid 公司id
     * @return java.lang.Integer
     */
    Integer updateBatch(List<CustomerUserEntity> list, String corpid) throws XbbException;

    Integer updateBatch(List<CustomerUserEntity> list, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;
    /**
     * 列表页显示前负责人
     * @param customerList es搜索的客户信息
     * @param corpid 公司id
     * @return
     * @author long.rao
     * @date 2019-03-25 10:01
     */
    List<PaasFormDataEntityExt> setLastMainPerson(List<PaasFormDataEntityExt> customerList, String corpid);

    /**
     * 重置前负责人
     * @param corpid 公司id
     * @param customerId 客户id
     */
    void initPreUser(String corpid, Long customerId) throws XbbException;

    /**
     * 重置前负责人
     * @param corpid 公司id
     * @param customerId 客户id
     */
    void initPreUser(List<CustomerUserEntity> customerUserEntityList) throws XbbException;

    /**
     * 通过corpid,userId,customerId获取客户销售团队记录
     * @param corpid 公司id
     * @param userId 员工userId
     * @param customerId 客户id
     * @return 客户销售团队记录
     */
    CustomerUserEntity getByUserIdCustomerId(String corpid, String userId, Integer customerId);

    /**
     * 批量更新团队状态
     *
     * @param corpid
     * @param dataId
     * @param updateUserIdIn
     * @param isMain
     * @throws XbbException
     */
    void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> userTeamEntityList, Integer isMain, Integer del) throws XbbException;

    void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> userTeamEntityList, Integer isMain, Integer del, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    List<Long> getDataIdForTempJob(String corpid, List<Long> customerIdList);

    /**
     * 获取团队
     * @param corpid
     * @param customerId
     * @return
     * @throws XbbException
     */
    List<CustomerUserEntity> getByCustomerId(String corpid, Long customerId);

    /**
     * 隔离模式下解散团队
     *
     * @param corpid
     * @param shouldUpdateUserTeamList
     * @param shouldUpdateIdList
     */
    void isolationDisbandedTeam(String corpid, List<UserTeamEntity> shouldUpdateUserTeamList, List<Long> shouldUpdateIdList) throws XbbException;

    /**
     * 更新协同人可编辑权限
     *
     * @param shouldUpdateEntity
     */
    void updateEditFormPer(CustomerUserEntity shouldUpdateEntity) throws XbbException;

    /**
     * 修改团队名称（隔离）
     *
     * @param customerUserEntity
     */
    void renameTeam(CustomerUserEntity customerUserEntity) throws XbbException;

    /**
     * 编辑团队成员标签
     *
     * @param customerUserEntity
     */
    void editLabel(CustomerUserEntity customerUserEntity) throws XbbException;

    /**
     * 批量更新团队中的协同人的负责人信息
     *
     * @param corpid 公司id
     * @param idIn 团队表中的主键id
     * @param updateUserTeamMainList 更新协同人的负责人信息
     */
    void batchUpdateTeamMainByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> updateUserTeamMainList) throws XbbException;

    void batchUpdateTeamMainByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> updateUserTeamMainList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 更新团队协同人信息和协同人可编辑权限
     *
     * @param corpid
     * @param idIn
     * @param updateUserToCoTeamList
     * @param coopUser
     * @param needThrow
     */
    void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> updateUserToCoTeamList, Integer coopUser, boolean needThrow) throws XbbException;

    void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;
}
